"""
题目要求，下载"安居客"网站 沭阳的房产信息

分析：
1. 先通过浏览器找到下一页数据的URL
2. 通过requests.get下载json数据
3. 通过jsonpath提取出所有的房产信息
4. 创建 csv文件，将字典写入到csv文件
"""

# 2. 下载json数据
import csv

import jsonpath
import requests

url = "https://shuyang.anjuke.com/v3/ajax/map/sale/3929/prop_list/?room_num=-1&price_id=-1&area_id=-1&floor=-1&orientation=-1&is_two_years=0&is_school=0&is_metro=0&order_id=0&p=2&zoom=12&lat=34.003089_34.231663&lng=118.327488_119.293346&kw=&et=5a5d22&ib=1&bst=pem654"
headers = {
    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
}
r = requests.get(url=url, headers=headers)
json_dict = r.json()
# print(json_dict)

# 3. 通过jsonpath提取数据
house_list = jsonpath.jsonpath(json_dict, "$..props[*]")

print("提取到的条数:", len(house_list))  # 正常情况下为60条
# print(house_list)

# 只提取出想要的数据
house_list_new = [
    {
        "region_name": x.get("region_name"),
        "block_name": x.get("block_name"),
        "rhval": x.get("rhval"),
        "area": x.get("area"),
        "price": x.get("price"),
        "long_title": x.get("long_title"),
        "comm_name": x.get("comm_name"),
        "house_orient_name": x.get("house_orient_name"),
        "floor_tag": x.get("floor_tag"),
    }
    for x in house_list
]

# 4. 创建 csv文件，将字典写入到csv文件
with open("沭阳房产_20201106.csv", "w") as f:
    headers = ["region_name", "block_name", "rhval", "area", "price", "long_title", "comm_name", "house_orient_name", "floor_tag"]
    csv_f = csv.DictWriter(f, headers)
    csv_f.writeheader()
    csv_f.writerows(house_list_new)
